#define _CRT_SECURE_NO_WARNINGS

/////////////////////////1
class Solution {
public:
    int lastStoneWeightII(vector<int>& stones) {
        int sum = 0, n = stones.size();
        for (auto& e : stones) sum += e;
        int aim = sum / 2;
        vector<vector<int>> dp(n + 1, vector<int>(aim + 1, 0));
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= aim; j++) {
                dp[i][j] = dp[i - 1][j];
                if (j >= stones[i - 1]) dp[i][j] = max(dp[i][j], dp[i - 1][j - stones[i - 1]] + stones[i - 1]);
            }
        return sum - dp[n][aim] - dp[n][aim];
    }
};
/////////////////////////1

/////////////////////////2
class Solution {
public:
    int lastStoneWeightII(vector<int>& stones) {
        int sum = 0, n = stones.size();
        for (auto& e : stones) sum += e;
        int aim = sum / 2;
        vector<int> dp(aim + 1, 0);
        for (int i = 1; i <= n; i++)
            for (int j = aim; j >= stones[i - 1]; j--) {
                dp[j] = max(dp[j], dp[j - stones[i - 1]] + stones[i - 1]);
            }
        return sum - dp[aim] - dp[aim];
    }
};
/////////////////////////2